import { Result } from 'ahooks/lib/useRequest/src/types'
import dayjs from 'dayjs'
import { DatePicker, Input, InputNumber, Radio, Select } from 'antd'

import { EventSelect } from '@oBiz/components/EventSelect'
import { IEventTemplateEnableRes } from '@oBiz/components/EventSelect/api'
import TimeZoneSelect from '@oBiz/components/TimeZoneSelect'
import { TAddressListRes } from '@oBiz/hooks/SWRApi/useAddressList'
import { getLogisticsProductServiceLevelName, getTailLineName } from '../../utils'
import { TFormItem, TOperationType } from './interface'

export const operationTypeTitleMap = {
  addEventTemplate: '新增事件模板',
  updateServiceLevel: '更新服务等级',
  updateFirstLine: '更新头程信息',
  updateTailLines: '更新尾程信息',
}

export const eventTemplateBatchAction = (eventList: IEventTemplateEnableRes[]): TFormItem[] => [
  {
    name: 'eventTemplate',
    label: '轨迹模板',
    component: EventSelect,
    componentProps: {
      outsideEventList: eventList,
      inputProps: {
        placeholder: '请选择轨迹模板',
      },
    },
  } satisfies TFormItem<typeof EventSelect>,
  {
    name: 'eventTime',
    label: '发生时间',
    component: DatePicker,
    componentProps: {
      disabledDate: current => current && current > dayjs().endOf('day'),
    },
  } satisfies TFormItem<typeof DatePicker>,
  {
    name: 'updateEdd',
    label: 'EDD',
    component: DatePicker,
    componentProps: {
      disabledDate: current => current && current > dayjs().endOf('day'),
    },
  } satisfies TFormItem<typeof DatePicker>,
  {
    name: 'updateEddZone',
    label: '时区',
    component: TimeZoneSelect,
  } satisfies TFormItem<typeof TimeZoneSelect>,
  {
    name: 'nextUpdateTime',
    label: '下次更新时间',
    component: DatePicker,
    componentProps: {
      disabledDate: current => current && current <= dayjs().endOf('day'),
    },
  } satisfies TFormItem<typeof DatePicker>,
  {
    name: 'note',
    label: '备注',
    component: Input,
    componentProps: {
      placeholder: '请输入备注',
      maxLength: 100,
    },
  } satisfies TFormItem<typeof Input>,
]

export const updateServiceLevelBatchAction: TFormItem[] = [
  {
    name: 'logisticsChannelServiceLevel',
    label: '服务等级',
    component: Radio.Group,
    componentProps: {
      block: true,
      buttonStyle: 'solid',
      optionType: 'button',
      options: getLogisticsProductServiceLevelName.valueList.map(item => ({
        label: getLogisticsProductServiceLevelName(item),
        value: item,
      })),
    },
  } satisfies TFormItem<typeof Radio.Group>,
  {
    name: 'logisticsChannelServiceTransitMin',
    label: '最短时效',
    component: InputNumber,
    componentProps: {
      placeholder: '请输入最短时效',
    },
  } satisfies TFormItem<typeof InputNumber>,
  {
    name: 'logisticsChannelServiceTransitMax',
    label: '最长时效',
    component: InputNumber,
    componentProps: {
      placeholder: '请输入最长时效',
    },
  } satisfies TFormItem<typeof InputNumber>,
]

export const updateFirstLineBatchAction = (addressList: Result<TAddressListRes, []>): TFormItem[] => [
  {
    name: 'carrier',
    label: '承运商',
    component: Input,
    componentProps: {
      placeholder: '请输入承运商',
    },
  } satisfies TFormItem<typeof Input>,
  {
    name: 'vesselName',
    label: '船名',
    component: Input,
    componentProps: {
      placeholder: '请输入船名',
    },
  } satisfies TFormItem<typeof Input>,
  {
    name: 'voyageNumber',
    label: '航次',
    component: Input,
    componentProps: {
      placeholder: '请输入航次',
    },
  } satisfies TFormItem<typeof Input>,
  {
    name: 'containerNumber',
    label: '柜号',
    component: Input,
    componentProps: {
      placeholder: '请输入柜号',
    },
  } satisfies TFormItem<typeof Input>,
  {
    name: 'departureLocationId',
    label: '起运港',
    component: Select,
    componentProps: {
      placeholder: '请选择起运港',
      options:
        addressList.data?.list.map(item => ({
          label: item.chineseName,
          value: item.addressId,
        })) || [],
    },
  } satisfies TFormItem<typeof Select>,
  {
    name: 'etd',
    label: 'ETD',
    component: DatePicker,
  } satisfies TFormItem<typeof DatePicker>,
  {
    name: 'destinationLocationId',
    label: '目的港',
    component: Select,
    componentProps: {
      placeholder: '请选择目的港',
      options:
        addressList.data?.list.map(item => ({
          label: item.chineseName,
          value: item.addressId,
        })) || [],
    },
  } satisfies TFormItem<typeof Select>,
  {
    name: 'eta',
    label: 'ETA',
    component: DatePicker,
  } satisfies TFormItem<typeof DatePicker>,
]

export const updateTailLinesBatchAction: TFormItem[] = [
  {
    name: 'lastMileTransportType',
    label: '尾程运输方式',
    component: Select,
    componentProps: {
      options: getTailLineName.valueList.map(item => ({
        label: getTailLineName(item),
        value: item,
      })),
      placeholder: '请选择尾程运输方式',
    },
  } satisfies TFormItem<typeof Select>,
  {
    name: 'lastMileCarrier',
    label: '尾程服务商',
    dependencies: ['lastMileTransportType'],
    component: ({ record, ...rest }) => {
      const lastMileTransportType = record?.lastMileTransportType
      return lastMileTransportType === 1 ? (
        <Select
          style={{ width: '100%' }}
          allowClear
          placeholder="请选择尾程服务商"
          options={['UPS', 'FedEx', 'DHL', 'USPS等'].map(item => ({
            label: item,
            value: item,
          }))}
          {...rest}
        />
      ) : (
        <Input
          style={{ width: '100%' }}
          placeholder="请输入尾程服务商"
          disabled={lastMileTransportType === 3}
          {...rest}
        />
      )
    },
    componentProps: {
      placeholder: '请选择尾程服务商',
    },
  } satisfies TFormItem<typeof Select>,
]

export const getBatchActionConfig = (
  operationType: TOperationType,
  addressList: Result<TAddressListRes, []>,
  eventList: IEventTemplateEnableRes[],
) => {
  switch (operationType) {
    case 'addEventTemplate':
      return eventTemplateBatchAction(eventList)
    case 'updateServiceLevel':
      return updateServiceLevelBatchAction
    case 'updateFirstLine':
      return updateFirstLineBatchAction(addressList)
    case 'updateTailLines':
      return updateTailLinesBatchAction
  }
}
